package base

// GetPageSliceIndex 根据page, size对切片进行分页, l 为目标切片的长度
func GetPageSliceIndex(page, size int32, l int) (startIndex, endIndex int32) {
	if l == 0 {
		return
	}

	if page <= 0 {
		page = 1
	}

	if size == 0 {
		size = 10
	}

	lInt32 := int32(l)
	lastIndex := lInt32 - 1
	startIndex = (page - 1) * size
	if startIndex > lastIndex {
		startIndex = lInt32
	}

	endIndex = startIndex + size
	if endIndex > lastIndex {
		endIndex = lInt32
	}
	return
}

// DistinctInt32Ids 去重
func DistinctInt32Ids(ids []int32) (res []int32) {
	hash := map[int32]struct{}{}
	for _, v := range ids {
		if _, ok := hash[v]; ok {
			continue
		}
		hash[v] = struct{}{}
		res = append(res, v)
	}
	return
}

// DistinctIds 去重
func DistinctIds(ids []string) []string {
	tmp := make(map[string]struct{}, len(ids))
	res := make([]string, 0, len(ids))

	for _, id := range ids {
		if _, ok := tmp[id]; !ok {
			tmp[id] = struct{}{}
			res = append(res, id)
		}
	}
	return res
}
